/*
 * Created on 2005/05/09
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.swt.graphics.Image;

/**
 * 
 *
 */
public class CachedImageFigure extends Figure{
    
    private String key;
    private CachedImageLoader loader;
    public CachedImageFigure(CachedImageLoader loader,String key,int width,int height){

        this.loader=loader;
    this.key=key;

    setSize(width,height);

    }

    public Image getImage(){
        return loader.loadImage(key);
    }
    
    public void paintFigure(Graphics g) {
    Image img=loader.loadImage(key);
    if(img!=null && !img.isDisposed()){
        int w=img.getBounds().width;
        int h=img.getBounds().height;
        if(w!=getClientArea().width || h!=getClientArea().height){
            setSize(w,h);
            update();
        }
       
        
        g.drawImage(img,getClientArea().x,getClientArea().y);
    	}
    }
    public void update(){
        /*
         * TODO image should have size.
        IFigure[] figures=(IFigure[]) getParent().getChildren().toArray(new IFigure[getParent().getChildren().size()]);
        getParent().getLayoutManager().layout(getParent());
        */
    }
}
